﻿package core.drawing
{
    import core.enum.*;
    import flash.events.*;

    final public class BrushTool extends ShapeTool
    {
        var n:int;
        var cx:int;
        var cy:int;
        var sx:int;
        var sy:int;
        var ux:int;
        var uy:int;
        var alpha:int;
        var a:int;
        var ta:int;
        var tr:int;
        var tg:int;
        var tb:int;
        var max:int;
        var px:uint;
        var pt:uint;

        public function BrushTool(param1:Tuyitu)
        {
            super(param1, ToolType.Brush);
            ctrl = ToolType.Picker;
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                this.UpdateReferences();
                this.Prepare(Action.BrushTool, layer.SetBitmapEditMode(options.opacity, canvas.clip));
                options.color = ed.toolbar.GetColor(1, layer.editMask);
                this.tr = options.color >>> 16 & 255;
                this.tg = options.color >>> 8 & 255;
                this.tb = options.color & 255;
                if (!event.shiftKey)
                {
                    var _loc_2:int = -1;
                    y = -1;
                    x = _loc_2;
                }
                this.MouseDrag(null);
            }
            return;
        }// end function

        override public function DrawShape(SetBitmapEditMode:int, SetBitmapEditMode:int) : void
        {
            this.n = 0;
            if (options.scatter)
            {
                SetBitmapEditMode = SetBitmapEditMode + int(Math.random() * shape.width - shape.width * 0.5);
                SetBitmapEditMode = SetBitmapEditMode + int(Math.random() * shape.height - shape.height * 0.5);
            }
            this.sy = SetBitmapEditMode - int(shape.height * 0.5);
            this.cy = this.sy + shape.height;
            while (this.sy < this.cy)
            {
                
                this.sx = SetBitmapEditMode - int(shape.width * 0.5);
                this.cx = this.sx + shape.width;
                while (this.sx < this.cx)
                {
                    
                    if (options.diffuse)
                    {
                        this.ux = int(Math.random() * shape.width * 0.25 - shape.width * 0.125);
                        this.uy = int(Math.random() * shape.height * 0.25 - shape.height * 0.125);
                    }
                    if (target.rect.contains(this.sx + this.ux, this.sy + this.uy))
                    {
                        var _loc_3:* = shape.data[this.n];
                        this.alpha = shape.data[this.n];
                        this.ta = _loc_3;
                        this.max = selection != null ? (selection.Max(this.sx + this.ux + offset.x, this.sy + this.uy + offset.y)) : (255);
                        if (this.ta > 0)
                        {
                            if (this.ta < this.max)
                            {
                                this.px = target.getPixel32(this.sx + this.ux, this.sy + this.uy);
                                this.a = this.px >>> 24 & 255;
                                if (this.a > 0)
                                {
                                    if (this.a == 255)
                                    {
                                        this.ta = 255;
                                    }
                                    else
                                    {
                                        this.ta = this.ta + (255 - this.alpha) * this.a / 255;
                                    }
                                }
                                if (this.ta > this.max)
                                {
                                    this.ta = this.max;
                                }
                                this.pt = this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb;
                                if (this.px != this.pt)
                                {
                                    target.setPixel32(this.sx + this.ux, this.sy + this.uy, this.pt);
                                }
                            }
                            else
                            {
                                if (this.ta > this.max)
                                {
                                    this.ta = this.max;
                                }
                                target.setPixel32(this.sx + this.ux, this.sy + this.uy, this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                            }
                        }
                    }
                    var _loc_3:String = this;
                    var _loc_4:* = this.n + 1;
                    _loc_3.n = _loc_4;
                    var _loc_3:String = this;
                    var _loc_4:* = this.sx + 1;
                    _loc_3.sx = _loc_4;
                }
                var _loc_3:String = this;
                var _loc_4:* = this.sy + 1;
                _loc_3.sy = _loc_4;
            }
            return;
        }// end function

    }
}
